package com.david.controller.api;

import com.david.entity.Article;
import com.david.entity.Category;
import com.david.model.api.Response;
import com.david.service.ArticleService;
import com.david.service.CategoryService;
import com.david.utils.ConvertUtils;
import com.google.common.collect.ImmutableBiMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @Autowired
    CategoryService categoryService;

    @Value("${fileLocation}")
    private String location;

    @GetMapping("findAll")
    @ApiOperation(value = "获取全部", notes = "获取全部", tags = "article")
    public Response<Object> findAll() {
        Response<Object> result = new Response<>();
        result.data = articleService.findAll();
        return result;
    }

    @GetMapping("findByParentId")
    @ApiOperation(value = "根据parentId获取", notes = "根据parentId获取", tags = "article")
    public Response<Object> findByParentId(Integer parentId) {
        Response<Object> result = new Response<>();
        result.data = articleService.findByParentId(parentId);
        return result;
    }


    @GetMapping("GetCategoryAndSelfByRoute")
    @ApiOperation(value = "筛选项包含自己", notes = "筛选项包含自己", tags = "article")
    public Response<Object> GetCategoryAndSelfByParentId(Integer parentId) {
        Response<Object> result = new Response<>();
        List<Category> oldlist = articleService.GetCategoryAndSelfByParentId(parentId);

        List<Category> newlist = new ArrayList<>();

        Category parent = oldlist.stream().filter(c->c.parentId == 0).collect(Collectors.toList()).get(0);
        parent.title = "全部";
        newlist.add(parent);

        for(Category c : oldlist){
            if(c.id == parentId){
                continue;
            }
            newlist.add(c);
        }

        result.data = newlist;
        return result;
    }
    @GetMapping("GetPageOfArticleByParentId")
    @ApiOperation(value = "分页", notes = "分页", tags = "article")
    public Response<Object> GetPageOfArticleByParentId(Integer parentId,String title,Integer page,Integer pageSize) {
        Response<Object> result = new Response<>();
        result.data = articleService.GetPageOfArticleByParentId(parentId,title,page,pageSize);
        return result;
    }

    @PostMapping("addArticle")
    @ApiOperation(value = "添加Article", notes = "添加Article", tags = "article")
    public Response<Map> addArticle(@RequestBody Article article) throws IOException {
        Response<Map> result = new Response<>();


        Category category = categoryService.findById(article.parentId);
        //合同
        if(category.parentId == 20){
            if(!article.fileSrc.equals("")){
                article.content = convertImg(article.fileSrc);
            }
        }

        int passResult = articleService.AddArticle(article);
        if (passResult > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "添加失败，请稍后再试");
        }
        return result;
    }

    public String convertImg(String fileSrc) throws IOException {
        fileSrc = fileSrc.substring(fileSrc.lastIndexOf('/')+1);
        String docPath = location + "/files/" + fileSrc;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSS");
        String formDate = sdf.format(date);

        String pdfFilePath = location + "/pdf/" + formDate + ".pdf";

        String content = ConvertUtils.officeConvertPdf(location,docPath,pdfFilePath);
        return content;
    }

    @GetMapping("GetArticleById")
    @ApiOperation(value = "GetArticleById", notes = "GetArticleById", tags = "article")
    public Response<Object> GetArticleById(Integer articleId) {
        Response<Object> result = new Response<>();
        result.data = articleService.GetArticleById(articleId);
        return result;
    }

    @PostMapping("updateArticle")
    @ApiOperation(value = "添加Article", notes = "添加Article", tags = "article")
    public Response<Map> updateArticle(@RequestBody Article article) throws IOException {
        Response<Map> result = new Response<>();

        Category category = categoryService.findById(article.parentId);
        //合同
        if(category.parentId == 20){
            article.content = convertImg(article.fileSrc);
        }

        int passResult = articleService.updateArticle(article);
        if (passResult > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "添加失败，请稍后再试");
        }
        return result;
    }

    @GetMapping("deleteArticle")
    @ApiOperation(value = "删除Article", notes = "删除Article", tags = "article")
    public Response<Map> deleteArticle(Integer id) {
        Response<Map> result = new Response<>();
        int passResult = articleService.deleteArticle(id);
        if (passResult > 0) {
            result.data = ImmutableBiMap.of("result", true);
        } else {
            result.data = ImmutableBiMap.of("result", false, "errMsg", "删除失败，请稍后再试");
        }
        return result;
    }


}
